package xju.rjxy.shop.service.impl;

import xju.rjxy.shop.constant.BaseConstant;
import xju.rjxy.shop.mapper.UserGameMapper;
import xju.rjxy.shop.mapper.impl.UserGameMapperImpl;
import xju.rjxy.shop.model.domain.UserGame;
import xju.rjxy.shop.model.vo.UserGameVO;
import xju.rjxy.shop.service.IUserGameService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户游戏关联服务接口实现类
 */
public class UserGameServiceImpl implements IUserGameService {
    private final UserGameMapper userGameMapper = new UserGameMapperImpl();

    /**
     * 获取用户游戏关联列表
     */
    @Override
    public List<UserGameVO> getUserGameList() {
        List<UserGameVO> userGameList = userGameMapper.selectUserGameList();
        if (userGameList != null && userGameList.size() > 0) {
            userGameList.forEach(userGame -> {
                // 处理状态文本值
                userGame.setFlagValue(getFlagValue(userGame.getFlag()));
            });
        }
        return userGameList;
    }
    
    /**
     * 根据用户ID获取用户游戏列表
     */
    @Override
    public List<UserGameVO> getUserGameListByUserId(Integer userId) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        List<UserGameVO> userGameList = userGameMapper.selectByUserId(userId);
        processUserGameList(userGameList);
        return userGameList;
    }
    
    /**
     * 根据好友ID获取好友游戏列表
     */
    @Override
    public List<UserGameVO> getFriendGameList(Integer friendId) {
        if (friendId == null) {
            throw new RuntimeException("好友ID不能为空");
        }
        
        List<UserGameVO> userGameList = userGameMapper.selectByFriendId(friendId);
        processUserGameList(userGameList);
        return userGameList;
    }
    
    /**
     * 添加用户游戏关联
     */
    @Override
    public boolean addUserGame(UserGame userGame) {
        if (userGame == null || userGame.getUserId() == null || userGame.getGameId() == null) {
            throw new RuntimeException("用户游戏信息不完整");
        }
        
        // 检查是否已存在
        UserGame existingUserGame = userGameMapper.selectByUserIdAndGameId(userGame.getUserId(), userGame.getGameId());
        if (existingUserGame != null) {
            throw new RuntimeException("用户已拥有该游戏");
        }
        
        // 设置默认值
        userGame.setPurchaseTime(LocalDateTime.now());
        userGame.setPlayCount(0);
        userGame.setPlayTime(0);
        userGame.setFlag(2); // 已购买状态
        userGame.setStatusCode("PURCHASED");
        
        int result = userGameMapper.insertUserGame(userGame);
        return result > 0;
    }
    
    /**
     * 更新用户游戏信息
     */
    @Override
    public boolean updateUserGame(UserGame userGame) {
        if (userGame == null || userGame.getId() == null) {
            throw new RuntimeException("用户游戏信息不完整");
        }
        
        int result = userGameMapper.updateUserGame(userGame);
        return result > 0;
    }
    
    /**
     * 删除用户游戏关联
     */
    @Override
    public boolean deleteUserGame(Integer id) {
        if (id == null) {
            throw new RuntimeException("用户游戏ID不能为空");
        }
        
        int result = userGameMapper.deleteUserGame(id);
        return result > 0;
    }
    
    /**
     * 根据用户ID和游戏ID查询
     */
    @Override
    public UserGame getUserGameByUserIdAndGameId(Integer userId, Integer gameId) {
        if (userId == null || gameId == null) {
            throw new RuntimeException("用户ID和游戏ID不能为空");
        }
        
        return userGameMapper.selectByUserIdAndGameId(userId, gameId);
    }
    
    /**
     * 获取用户游戏时长统计
     */
    @Override
    public Integer getTotalPlayTime(Integer userId) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        return userGameMapper.getTotalPlayTime(userId);
    }
    
    /**
     * 获取用户最近游玩游戏列表
     */
    @Override
    public List<UserGameVO> getRecentPlayedGames(Integer userId, Integer limit) {
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        if (limit == null || limit <= 0) {
            limit = 5; // 默认返回5个
        }
        
        List<UserGameVO> userGameList = userGameMapper.selectRecentPlayedGames(userId, limit);
        processUserGameList(userGameList);
        return userGameList;
    }

    private String getFlagValue(Integer flag) {
        if (flag == null) {
            return BaseConstant.EMPTY;
        }

        // hard code
        switch (flag) {
            case 1:
                return "未激活";
            case 2:
                return "已购买";
            case 3:
                return "已退款";
            case 4:
                return "已封禁";
            default:
                return BaseConstant.EMPTY;
        }
    }
    
    /**
     * 处理用户游戏列表，设置状态文本值
     */
    private void processUserGameList(List<UserGameVO> userGameList) {
        if (userGameList != null && userGameList.size() > 0) {
            userGameList.forEach(userGame -> {
                userGame.setFlagValue(getFlagValue(userGame.getFlag()));
            });
        }
    }
} 